/**************************************************************************
 * Copyright?2004-2016 浙江盘石信息技术股份有限公司
 * All rights reserved.
 *
 * 项目名称：互金网络平台
 * 版权说明：本软件属浙江盘石信息技术股份有限公司所有，在未获浙江盘石信息技术股份有限公司正式授权情况下，
 *          任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。   
 ***************************************************************************/
package com.example.oss.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.oss.returns.Result;
import com.example.oss.service.AliFileUploadService;
import com.example.oss.service.OssCallbackService;
import com.example.oss.service.ResourceInfoOperateService;
import com.example.oss.vo.AliOssTokenResVO;
import com.example.oss.vo.OssCallbackResVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jdk.nashorn.internal.ir.annotations.Reference;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 阿里OSS上传API操作
 */
@Slf4j
@RestController
@RequestMapping("/ali-oss/")
@Api(tags = "阿里OSS存储相关控制器")
public class AliOSSUploadController {


    @ApiOperation("工程测试")
    @GetMapping(value = "get-test")
    public Object getTest(){
        return "测试";
    }

    @Autowired
    private AliFileUploadService aliFileUploadService;

    @Autowired
    private OssCallbackService ossCallbackService;

    @Autowired
    private ResourceInfoOperateService ossResourceOperateService;


    /**
     * 获取OSS直传签名
     * @param uploadType 类型 0-视频 1-音频 2-照片
     * @return 返回
     */
    @ApiOperation("获取OSS直传签名")
    @GetMapping(value = "get-token")
    public Result getUploadToken(@ApiParam("类型 1-图片 2-我的图标") @RequestParam("uploadType") Integer uploadType) throws Exception {

        System.out.println(uploadType);

        AliOssTokenResVO aliOssTokenResVO = aliFileUploadService.getUploadToken(uploadType);
        return new Result("获取成功",aliOssTokenResVO);
    }

    /**
     * 阿里OSS上传回调
     * @return 返回
     */
    @ApiIgnore()
    @PostMapping("oss-callback")
    public Result ossCallback(HttpServletRequest request, HttpServletResponse response)throws Exception {
        String uri = request.getRequestURI();
        String queryString = request.getQueryString();
        String authorizationInput = request.getHeader("Authorization");
        String pubKeyInput = request.getHeader("x-oss-pub-key-url");

        log.info("uri信息为：[{}],queryString：[{}],authorizationInput：[{}],pubKeyInput：[{}]",uri,queryString,authorizationInput,pubKeyInput);

        //获取消息体
        String ossCallbackBody = ossCallbackService
                .GetPostBody(request.getInputStream(), Integer.parseInt(request.getHeader("content-length")));

        log.info("content-length 的数据为：[{}]",request.getHeader("content-length"));
        // 验证上传回调的数据
        boolean isValid = ossCallbackService
                .VerifyOSSCallbackData(uri, queryString, authorizationInput, pubKeyInput, ossCallbackBody);
        if (!isValid) {
            return new Result("上传回调验签失败");
        }

        JSONObject jsonObject = JSONUtil.parseObj(ossCallbackBody);
        String relativeUrl = jsonObject.getStr("filename");
        Long size = jsonObject.getLong("size");
        String mimeType = jsonObject.getStr("mimeType");
        Integer resourceType = jsonObject.getInt("resourceType");

        // 保存资源信息
        HashMap retMap = ossResourceOperateService.saveResource(relativeUrl, size, mimeType, resourceType);
        // String src = (String)retMap.get("src");
        // Integer resourceId = (Integer)retMap.get("resourceId");

        // OssCallbackResVO ossCallbackResVo = new OssCallbackResVO();
        // ossCallbackResVo.setSrc(src);
        // ossCallbackResVo.setResourceId(resourceId);

        // return new Result("",ossCallbackResVo);
        return new Result("获取消息体成功",jsonObject);
    }



}
